# @Time:   2021/2/3 14:45
# @Author: Zong Wei


import random
import time
from time import sleep

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait

from ui_automation_testing.test.pageObject.loginPage import LoginPage
from ui_automation_testing.utils.logger import Logger


class ExchangeRatePage(LoginPage):
    logger = Logger(__name__).get_log()
    month = '202103'
    more_button = (By.XPATH, "(//span[@class='caret'])[1]")
    base_menu = (By.XPATH, "//ul[@class='dropdown-menu']//span[.='基础资料']")
    menu = (By.XPATH, "//ul[@class='site-menu']//span[.='财务相关']")
    sub_menu = (By.XPATH, "(//li[@class='site-menu-item']//span[.='汇率维护'])[2]")
    search_button = (By.ID, "lr-search")
    add_button = (By.ID, "lr-add")
    edit_button = (By.ID, "lr-edit")
    del_button = (By.ID, "lr-delete")

    origin = (By.ID, "SCurrTypeCode")
    target = (By.ID, "DCurrTypeCode")
    biz_month = (By.ID, "BookingMonth")
    rate = (By.ID, "Exchange_Rate")

    confirm_button = (By.XPATH, "//a[.='确认']")
    first_row = (By.XPATH, "//span[.='原币']//following::tr[1]")

    def __init__(self):
        self.driver = LoginPage().login()
        self.wait = WebDriverWait(self.driver, 10)

    def exchange_rate_home(self):
        self.logger.info("登录系统成功")
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.more_button)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.base_menu)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.menu)).click()
        self.wait.until(ec.element_to_be_clickable(self.sub_menu)).click()
        sleep(1)
        return self.driver

    def add_rate(self, orgin='RMB', target='USD', rate='6.4591'):
        sleep(1)
        self.driver.switch_to.frame(1)
        self.wait.until(ec.element_to_be_clickable(self.add_button)).click()
        self.logger.info("添加汇率")
        sleep(2)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(3)
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.origin)).send_keys(orgin)
        self.wait.until(ec.presence_of_element_located(self.target)).send_keys(target)
        self.wait.until(ec.presence_of_element_located(self.biz_month)).send_keys(self.month)
        self.wait.until(ec.presence_of_element_located(self.rate)).send_keys(rate)
        sleep(1)
        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        sleep(1)
        self.logger.info("新增完成")

    def search_rate(self, biz_month=month):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.presence_of_element_located(self.biz_month)).send_keys(biz_month)
        self.wait.until(ec.element_to_be_clickable(self.search_button)).click()
        sleep(2)
        self.logger.info("查询完成")

    def edit_rate(self):
        self.search_rate()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.edit_button)).click()
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(3)
        self.wait.until(ec.presence_of_element_located(self.biz_month)).clear()
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.biz_month)).send_keys(time.strftime('%Y%m'))
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.rate)).clear()
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.rate)).send_keys(format(random.random()+6, '.4f'))
        sleep(1)
        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        self.logger.info("编辑修改完成")

    def del_rate(self):
        self.search_rate(time.strftime('%Y%m'))
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.del_button)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        self.logger.info("删除成功")


if __name__ == '__main__':
    er = ExchangeRatePage()
    er.exchange_rate_home()
    er.del_rate()

